import GameConst from "../GameConst";
import SoundsManager from "../soundmanager";
import Menu from "./Menu";
import Utils from "../utils";
import AnimatedNode from "../amn/AnimatedNode";
import BitmapText from "../BitmapText";
import ClickableObject from "../ClickableObject";
import SoundButton from "../SoundsButton";
import ButtonObject from "../ButtonObject";
import GameObject from "../GameObject";
export default class MapMenu extends Menu {
    constructor(manger) {
        super();
        var n = this;
        this.manager = manger;
        this.firstShow = true;
        this.isMoving = false;
        this.pointerIsMoving = false;
        this.currentLevel = 0;
        this.downPos = new Laya.Point;
        this.speed = 0;
        this.isDown = false;
        this.dragSpeed = 0;
        this.lastSpeed = 0;
        this.downY = 0;
        this.mainSprites = [];
        this.buttons = [];
        GameConst.levelToUnlock = -1;
        this.isLevelUnlocking = false;
        this.isPlayPressed = false;
        this.pointerSprites = [];
        this.sprite = new Laya.Sprite;
        this.scrollSprite = new Laya.Sprite;
        this.scrollSprite.name = "scrollSprite";
        this.sprite.addChild(this.scrollSprite);
        var heightOffset = [ 1320, 660, 0];
        this.height = 1320+200;
        for (var i = 1; i <= 3; ++i) {
            var img = new Laya.Image("game/map"+i+".jpg");
            //var height = ;
            //var imgHight = img.height;
            img.y = heightOffset[i - 1];
            this.scrollSprite.addChild(img);
            //totalH += imgHight + height;
            this.mainSprites.push(img);
        }
        this.rect = this.scrollSprite.getBounds();
        for (s = 0; s < GameConst.LEVEL_AMOUNT; ++s) {
            var btn = GameConst.mapButtons[s];//[154,1952,true,1,1]
            var name = btn[2] ? (s <= 5 ? "point_big_pinc" : "point_big_blue") : (s <= 5 ? "point_small_pinc" : "point_small_blue");
            var f = Utils.createBitmap( name );
            f.x = btn[0];
            f.y = btn[1];

            f.pivotX = f.width / 2;
            f.pivotY = f.height / 2;
            f.scaleX = btn[3];
            f.scaleY = btn[4];
            this.scrollSprite.addChild(f);
            this.buttons.push(f);
        }
        var c = new AnimatedNode(GameConst.animationManager.getAnimation("pointer"), 1 / 30, null);
        c.stop();
        c.x = MapMenu.buttonPositions[GameConst.episode][0][0];
        c.y = MapMenu.buttonPositions[GameConst.episode][0][1];
        this.scrollSprite.addChild(c);
        c.addAction(c.totalFrames - 1, 0);
        this.pointer = c;
        var h = new BitmapText("99",GameConst.BITMAPFONT_DEFAULT);
        h.text = "99";
        h.letterSpacing = -10;
        h.y = -70;
        this.pointer.getPart(0).addChild(h);
        this.pointerSprites.push(h);
        for (var s = 0; s < 3; ++s) {
            var f = Utils.createSpriteFromSpritesheet("star");
            this.pointer.getPart(0).addChild(f);
            f.pivotX = f.getBounds().width / 2;
            f.pivotY = f.getBounds().height / 2;
            f.scaleX = f.scaleY = .36 + s * .08;
            f.x = s == 0 ? -47 : s == 1 ? 0 : 52;
            f.y = -110 + (s == 1 ? -11 : s == 0 ? 5 : 0);
            f.rotation = (s - 1) * 25;
            this.pointerSprites.push(f);
        }
        
        var t_icon = Utils.createSpriteFromSpritesheet("target_ico");
        t_icon.pivotX = t_icon.width/2;
        t_icon.pivotY = t_icon.height/2;
        this.pointer.getPart(0).addChild(t_icon);
        this.pointerSprites.push(t_icon);
        t_icon.x = 25;
        t_icon.scale(0.5,0.5);
        this.scrollSprite.y = 0;
        for (var s = 0; s < this.pointerSprites.length; ++s){
            this.pointerSprites[s].mouseEnabled = false;
        }
        var p = new ClickableObject(this.pointer);
        p.setCircle(85, -2, -192);
        p.callback = function (e) {
            n.onPointerDown();
        };
        this.clickables.push(p);
        var button = new SoundButton(2, this.sprite, 670, 200);
        this.animatedButtons.push(button);
        this.clickables.push(button);
        var button = new SoundButton(0, this.sprite, 570, 215 - 50);
        this.animatedButtons.push(button);
        this.clickables.push(button);

        // this.brandLogo = new LogoObject(this.sprite, GameConst.SCREEN_W / 2, 0, 1);
        // this.clickables.push(this.brandLogo);
        this.onResize();
    }

    show() {
        super.show();
        var t = this;
        this.onResize();
        this.isLevelUnlocking = GameConst.levelToUnlock > 0;
        this.isPlayPressed = false;
        this.isMoving = false;
        this.pointerIsMoving = false;
        this.speed = 0;
        this.isDown = false;
        this.update(0);
        // this.sprite.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        // this.sprite.on(Laya.Event.MOUSE_UP,this, this.onPressUp);
        // this.sprite.on(Laya.Event.CLICK,this, this.onClick);
        // this.sprite.on(Laya.Event.MOUSE_WHEEL,this, this.onPressMove);
        this.firstShow = GameConst.levelManager.isFirstLoad && false;
        if (this.firstShow) {
            GameConst.levelManager.isFirstLoad = false;
            this.firstShow = false;
            this.scrollSprite.y = 0;
            var n = 1;
            this.scroll(GameConst.ACTUAL_H - this.height, 4e3 / n, 1e3 / n);
        } else if (this.isLevelUnlocking) {
            var r = this.buttons[GameConst.levelToUnlock];
            Laya.Tween.to(r,{
                scaleX: r.scaleX * 1.4,
                scaleY: r.scaleY * 1.4,
                alpha: .1
            }, 500, Laya.Ease.cubicOutLaya.Handler.create(this,this.onButtonHide));
        } else {
            this.movePoinPointerToButton(GameConst.levelManager.lastOpened);
        }
        this.updatePointerData();
        this.lastClickDt = Laya.timer.currTimer;
    };

    limitY (y) {
        var b = this.sprite.getBounds();
        var min = GameConst.FULL_SCREEN_H - b.height;
        if (y < min){
            y = min;
        }
        else if (y > 0){
            y = 0;
        }
        return y;
    };

    scroll (e, t, n) {
        var r = this;
        if (typeof n === "undefined") {
            n = 0;
        }
        if (this.isMoving)
            return;
        this.isMoving = true;
        Laya.Tween.clearAll(this.scrollSprite);
        this.speed = 0;
        Laya.Tween.to(this.scrollSprite,{
            x: 0,
            y: this.limitY(e)
        }, t, Laya.Ease.cubicInOut,Laya.Handler.create(this,this.stopMove));
    };

    destroy() {
        suuper.destroy();
    };
    onDown(x,y) {
        super.onDown(x,y);
        if (this.isLevelUnlocking)
            return;
        this.downPos = this.scrollSprite.globalToLocal(x, y);
        this.isDown = true;
        this.dragSpeed = 0;
        this.downY = y;
        //e.nativeEvent && e.preventDefault && e.preventDefault();
    };

    onUp(x,y) {
        super.onUp(x,y);
        if (this.isLevelUnlocking)
            return;
        this.isDown = false;
        this.speed = this.lastSpeed;
        var t = Utils.sign(this.speed);
        this.speed = t * Utils.limit(Math.abs(this.speed), 0, 15);
        Laya.Tween.to(this, {
            speed: 0
        }, 600, Laya.Ease.cubicOut);
        //e.nativeEvent && e.preventDefault && e.preventDefault();
        this.onClick(x,y);
        this.lastClickDt = Laya.timer.currTimer;
    };

    update(t) {
        super.update(t);
        if (this.isDown){
            var offset = Laya.stage.mouseY - this.downY;
            if(Math.abs(offset)>10){
                this.speed = offset/10;
            }else{
                this.speed = 0;
            }
            this.scrollSprite.y = this.limitY(this.scrollSprite.y + this.speed);
        }else{
            this.scrollSprite.y = this.limitY(this.scrollSprite.y - this.speed);
        }
        // for (var n = 0; n < 3; ++n) {
        //     var r = this.mainSprites[n];
        // }
        for (var n = 0; n < GameConst.LEVEL_AMOUNT; ++n) {
            var i = this.buttons[n];
            if(!i){
                return;
            }
            var s = i.localToGlobal(0, 0);
            i.visible = n == GameConst.levelToUnlock || GameConst.levelManager.data[n].state == GameConst.CLOSED_STATE && !(s.y > GameConst.ACTUAL_H + 200 || s.y < -200);
        }
        this.pointer.update(t);
        if(this.lastClickDt+3000 < Laya.timer.currTimer){
            this.lastClickDt = Laya.timer.currTimer+5000;
            this.pointer.gotoAndPlay(0);
        }
    };

    hide () {
        this.sprite.releaseMouseEvent;
        super.hide();
        //this.pointer.getPart(0).releaseMouseEvent();
    };
    
}
MapMenu.prototype.onResize = function () {
    for (var i = 1; i <= 3; ++i) {
        var click = this.clickables[i];
        if(click && click.sprite){
            this.clickables[i].sprite.x = 600 + (i - 2) * 75;
            this.clickables[i].sprite.y = 40 + (GameConst.FULL_SCREEN_H - GameConst.ACTUAL_H) / 2 + (i == 3 ? -2 : 0);
            this.clickables[i].sprite.scaleX = this.clickables[i].sprite.scaleY = i == 3 ? .55 : .75;
            this.clickables[i].updateRectScale();
        }
       
    }
    // if (this.brandLogo.sprite)
    //     this.brandLogo.sprite.y = GameConst.ACTUAL_H - 30 + (GameConst.FULL_SCREEN_H - GameConst.ACTUAL_H) / 2;
};
// MapMenu.prototype.loadMain = function (e) {
//     manager.show(manager.mainMenu);
// };
MapMenu.prototype.updatePointerData = function () {
    var e = GameConst.levelManager.data[this.currentLevel];
    var t = e.stars;
    var n = e.targetSpriteName;
    var r = this.pointerSprites[0];
    r.text = (e.levelNumber + 1).toString();
    r.pivotX = r.getBounds().width / 2;
    r.pivotY = r.getBounds().height / 2;
    for (var i = 0; i < 3; ++i) {
        var s = this.pointerSprites[i + 1];
        s.visible = i < t;
    }
    s = this.pointerSprites[4];
    s.gotoAndStop(n);
    s.scale(1,1);
    var bound = s.getBounds();
    s.pivotX = bound.width / 2;
    s.pivotY = bound.height / 2;
    s.scaleX = s.scaleY = Math.min(45 / bound.width, 45 / bound.height);
};

MapMenu.prototype.fastUnlock = function (e) {
    GameConst.levelToUnlock = -1;
    this.buttons[e].visible = false;
    Utils.removeClip(this.buttons[e]);
};
MapMenu.prototype.onButtonHide = function () {
    this.movePoinPointerToButton(GameConst.levelToUnlock);
};



MapMenu.prototype.onClick = function (stageX,stageY) {
    var sp = this.pointer;
    var pp = sp.localToGlobal(Utils.cjp(0,-180));
    var distance = Utils.distanceBetweenPoints(stageX,stageY,pp.x,pp.y);

    if(distance<80){
        this.onPointerDown();
        return;
    }
    if (this.isLevelUnlocking || this.isPlayPressed)
        return;
    if (!this.isMoving && !this.pointerIsMoving) {
        var localPoint = this.scrollSprite.globalToLocal(Utils.cjp(stageX, stageY));
        var len = MapMenu.buttonPositions[GameConst.episode].length;
        for (var i = 0; i < len; ++i) {
            var position = MapMenu.buttonPositions[GameConst.episode][i];
            if (Utils.distanceBetweenPoints(localPoint.x, localPoint.y, position[0], position[1]) < MapMenu.BUTTON_RADIUS) {
                if (GameConst.levelManager.data[i].state == GameConst.CLOSED_STATE)
                    return;
                this.movePoinPointerToButton(i);
                break
            }
        }
    }
};
MapMenu.prototype.movePoinPointerToButton = function (e) {
    var p = MapMenu.buttonPositions[GameConst.episode][e];
    this.pointerIsMoving = true;
    this.currentLevel = e;
    this.pointer.gotoAndPlay(0);
    Laya.Tween.to(this.pointer, {
        x: p[0],
        y: p[1]
    }, 300 * 1.4 * .7, Laya.Ease.cubicInOut,Laya.Handler.create(this,this.stopPointerMove));
    var i = p[1] - (275 + 50);
    var s = p[1] + 50;
    if (this.scrollSprite.y + i < 0)
        this.scroll(-i + 100, 300, 0);
    else if (this.scrollSprite.y + s > GameConst.SCREEN_H + GameConst.SHIFT_H)
        this.scroll(GameConst.SCREEN_H + GameConst.SHIFT_H - s - 100, 300, 0);
    this.updatePointerData();
    SoundsManager.instance.playSound("pointer_sound");
};

MapMenu.prototype.onPressMove = function (e) {
    if (this.isLevelUnlocking)
        return;
    if (this.isMoving)
        return;
    var t = this.scrollSprite.localToGlobal(Utils.cjp(this.downPos.x, this.downPos.y));
    var n = this.scrollSprite.y;
    this.scrollSprite.y = this.limitY(this.scrollSprite.y + e.stageY - t.y);
    this.lastSpeed = this.scrollSprite.y - n;
    e.nativeEvent && e.preventDefault && e.preventDefault();
};

MapMenu.prototype.onPointerDown = function () {
    if (this.isLevelUnlocking || this.isPlayPressed)
        return;
    if (!this.pointerIsMoving) {
        this.pointer.gotoAndPlay(0);
        GameConst.levelManager.prepareToLoadLevel(this.currentLevel);
        SoundsManager.instance.playSound("play_button");
        this.isPlayPressed = true;
        return;
    }
};
MapMenu.prototype.stopPointerMove = function () {
    this.pointerIsMoving = false;
    this.isLevelUnlocking = false;
    GameConst.levelToUnlock = -1;
    if (!this.pointer.isPlaying)
        this.pointer.gotoAndPlay(0);
};
MapMenu.prototype.stopMove = function () {
    this.isMoving = false;
};
MapMenu.buttonPositions = [null, [[184, 1921], [268, 1880], [328, 1826], [370, 1769], [372, 1702], [360, 1659], [313, 1623], [265, 1584], [207, 1558], [140, 1519], [171, 1473], [248, 1461], [340, 1443], [337, 1385], [305, 1319], [332, 1276], [386, 1249], [454, 1236], [522, 1206], [546, 1149], [535, 1103], [492, 1073], [432, 1043], [372, 1023], [289, 1006], [182, 993], [124, 976], [92, 928], [103, 875], [314, 793], [413, 802], [505, 783], [568, 735], [552, 674], [514, 610]], [[509, 1918], [453, 1882], [404, 1841], [377, 1782], [371, 1708], [356, 1660], [354, 1600], [356, 1544], [359, 1487], [371, 1429], [362, 1390], [336, 1349], [336, 1310], [371, 1278], [421, 1236], [485, 1221], [532, 1206], [564, 1181], [571, 1142], [548, 1102], [519, 1070], [477, 1048], [421, 1028], [376, 1015], [324, 987], [264, 980], [203, 959], [147, 898], [176, 856], [229, 813], [301, 797], [356, 775], [400, 734], [374, 689], [348, 637]], [[155, 1937], [201, 1901], [236, 1860], [228, 1804], [201, 1743], [190, 1645], [335, 1618], [394, 1592], [430, 1545], [409, 1482], [391, 1444], [340, 1407], [278, 1375], [221, 1348], [163, 1297], [119, 1264], [99, 1224], [99, 1174], [124, 1130], [160, 1085], [213, 1052], [274, 1037], [337, 1038], [389, 1056], [452, 1076], [524, 1056], [567, 1019], [571, 965], [542, 914], [486, 860], [419, 850], [365, 841], [313, 820], [280, 787], [251, 735], [206, 705], [191, 663], [210, 617], [263, 587], [263, 493]], [[353, 1747], [365, 1697], [338, 1636], [285, 1594], [273, 1528], [290, 1487], [332, 1455], [361, 1411], [368, 1369], [358, 1315], [352, 1276], [333, 1235], [308, 1200], [274, 1165], [240, 1122], [202, 1088], [159, 1055], [118, 1012], [96, 967], [85, 910], [86, 866], [104, 828], [135, 793], [172, 762], [233, 729], [306, 735], [356, 751], [394, 778], [418, 814], [450, 839], [517, 820], [558, 786], [565, 743], [530, 701], [480, 648], [415, 639], [361, 628], [324, 605], [311, 571], [315, 516]]];
MapMenu.BUTTON_RADIUS = 30;
